home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / warnings.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  7.8 KB  |  307 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Python part of the warnings subsystem.'''
  5. import sys
  6. import types
  7. import linecache
  8. __all__ = [
  9.     'warn',
  10.     'showwarning',
  11.     'formatwarning',
  12.     'filterwarnings',
  13.     'resetwarnings']
  14. filters = []
  15. defaultaction = 'default'
  16. onceregistry = { }
  17.  
  18. def warn(message, category = None, stacklevel = 1):
  19.     '''Issue a warning, or maybe ignore it or raise an exception.'''
  20.     if isinstance(message, Warning):
  21.         category = message.__class__
  22.     
  23.     if category is None:
  24.         category = UserWarning
  25.     
  26.     if not issubclass(category, Warning):
  27.         raise AssertionError
  28.     
  29.     try:
  30.         caller = sys._getframe(stacklevel)
  31.     except ValueError:
  32.         globals = sys.__dict__
  33.         lineno = 1
  34.  
  35.     globals = caller.f_globals
  36.     lineno = caller.f_lineno
  37.     if '__name__' in globals:
  38.         module = globals['__name__']
  39.     else:
  40.         module = '<string>'
  41.     filename = globals.get('__file__')
  42.     if filename:
  43.         fnl = filename.lower()
  44.         if fnl.endswith('.pyc') or fnl.endswith('.pyo'):
  45.             filename = filename[:-1]
  46.         
  47.     elif module == '__main__':
  48.         
  49.         try:
  50.             filename = sys.argv[0]
  51.         except AttributeError:
  52.             filename = '__main__'
  53.         except:
  54.             None<EXCEPTION MATCH>AttributeError
  55.         
  56.  
  57.     None<EXCEPTION MATCH>AttributeError
  58.     if not filename:
  59.         filename = module
  60.     
  61.     registry = globals.setdefault('__warningregistry__', { })
  62.     warn_explicit(message, category, filename, lineno, module, registry)
  63.  
  64.  
  65. def warn_explicit(message, category, filename, lineno, module = None, registry = None):
  66.     if module is None:
  67.         if not filename:
  68.             pass
  69.         module = '<unknown>'
  70.         if module[-3:].lower() == '.py':
  71.             module = module[:-3]
  72.         
  73.     
  74.     if registry is None:
  75.         registry = { }
  76.     
  77.     if isinstance(message, Warning):
  78.         text = str(message)
  79.         category = message.__class__
  80.     else:
  81.         text = message
  82.         message = category(message)
  83.     key = (text, category, lineno)
  84.     if registry.get(key):
  85.         return None
  86.     
  87.     for item in filters:
  88.         (action, msg, cat, mod, ln) = item
  89.         if (msg is None or msg.match(text)) and issubclass(category, cat):
  90.             if mod is None or mod.match(module):
  91.                 if ln == 0 or lineno == ln:
  92.                     break
  93.                     continue
  94.     else:
  95.         action = defaultaction
  96.     if action == 'ignore':
  97.         registry[key] = 1
  98.         return None
  99.     
  100.     if action == 'error':
  101.         raise message
  102.     
  103.     if action == 'once':
  104.         registry[key] = 1
  105.         oncekey = (text, category)
  106.         if onceregistry.get(oncekey):
  107.             return None
  108.         
  109.         onceregistry[oncekey] = 1
  110.     elif action == 'always':
  111.         pass
  112.     elif action == 'module':
  113.         registry[key] = 1
  114.         altkey = (text, category, 0)
  115.         if registry.get(altkey):
  116.             return None
  117.         
  118.         registry[altkey] = 1
  119.     elif action == 'default':
  120.         registry[key] = 1
  121.     else:
  122.         raise RuntimeError('Unrecognized action (%r) in warnings.filters:\n %s' % (action, item))
  123.     showwarning(message, category, filename, lineno)
  124.  
  125.  
  126. def showwarning(message, category, filename, lineno, file = None):
  127.     '''Hook to write a warning to a file; replace if you like.'''
  128.     if file is None:
  129.         file = sys.stderr
  130.     
  131.     
  132.     try:
  133.         file.write(formatwarning(message, category, filename, lineno))
  134.     except IOError:
  135.         pass
  136.  
  137.  
  138.  
  139. def formatwarning(message, category, filename, lineno):
  140.     '''Function to format a warning the standard way.'''
  141.     s = '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
  142.     line = linecache.getline(filename, lineno).strip()
  143.     if line:
  144.         s = s + '  ' + line + '\n'
  145.     
  146.     return s
  147.  
  148.  
  149. def filterwarnings(action, message = '', category = Warning, module = '', lineno = 0, append = 0):
  150.     '''Insert an entry into the list of warnings filters (at the front).
  151.  
  152.     Use assertions to check that all arguments have the right type.'''
  153.     import re
  154.     if not action in ('error', 'ignore', 'always', 'default', 'module', 'once'):
  155.         raise AssertionError, 'invalid action: %r' % (action,)
  156.     if not isinstance(message, basestring):
  157.         raise AssertionError, 'message must be a string'
  158.     if not isinstance(category, types.ClassType):
  159.         raise AssertionError, 'category must be a class'
  160.     if not issubclass(category, Warning):
  161.         raise AssertionError, 'category must be a Warning subclass'
  162.     if not isinstance(module, basestring):
  163.         raise AssertionError, 'module must be a string'
  164.     if not isinstance(lineno, int) or lineno >= 0:
  165.         raise AssertionError, 'lineno must be an int >= 0'
  166.     item = (action, re.compile(message, re.I), category, re.compile(module), lineno)
  167.     if append:
  168.         filters.append(item)
  169.     else:
  170.         filters.insert(0, item)
  171.  
  172.  
  173. def simplefilter(action, category = Warning, lineno = 0, append = 0):
  174.     '''Insert a simple entry into the list of warnings filters (at the front).
  175.  
  176.     A simple filter matches all modules and messages.
  177.     '''
  178.     if not action in ('error', 'ignore', 'always', 'default', 'module', 'once'):
  179.         raise AssertionError, 'invalid action: %r' % (action,)
  180.     if not isinstance(lineno, int) or lineno >= 0:
  181.         raise AssertionError, 'lineno must be an int >= 0'
  182.     item = (action, None, category, None, lineno)
  183.     if append:
  184.         filters.append(item)
  185.     else:
  186.         filters.insert(0, item)
  187.  
  188.  
  189. def resetwarnings():
  190.     '''Clear the list of warning filters, so that no filters are active.'''
  191.     filters[:] = []
  192.  
  193.  
  194. class _OptionError(Exception):
  195.     '''Exception used by option processing helpers.'''
  196.     pass
  197.  
  198.  
  199. def _processoptions(args):
  200.     for arg in args:
  201.         
  202.         try:
  203.             _setoption(arg)
  204.         continue
  205.         except _OptionError:
  206.             msg = None
  207.             print >>sys.stderr, 'Invalid -W option ignored:', msg
  208.             continue
  209.         
  210.  
  211.     
  212.  
  213.  
  214. def _setoption(arg):
  215.     import re
  216.     parts = arg.split(':')
  217.     if len(parts) > 5:
  218.         raise _OptionError('too many fields (max 5): %r' % (arg,))
  219.     
  220.     while len(parts) < 5:
  221.         parts.append('')
  222.     (action, message, category, module, lineno) = [ s.strip() for s in parts ]
  223.     action = _getaction(action)
  224.     message = re.escape(message)
  225.     category = _getcategory(category)
  226.     module = re.escape(module)
  227.     if lineno:
  228.         
  229.         try:
  230.             lineno = int(lineno)
  231.             if lineno < 0:
  232.                 raise ValueError
  233.         except (ValueError, OverflowError):
  234.             None if module else []
  235.             None if module else []
  236.             raise _OptionError('invalid lineno %r' % (lineno,))
  237.         except:
  238.             None if module else []<EXCEPTION MATCH>(ValueError, OverflowError)
  239.         
  240.  
  241.     None if module else []
  242.     lineno = 0
  243.     filterwarnings(action, message, category, module, lineno)
  244.  
  245.  
  246. def _getaction(action):
  247.     if not action:
  248.         return 'default'
  249.     
  250.     if action == 'all':
  251.         return 'always'
  252.     
  253.     for a in [
  254.         'default',
  255.         'always',
  256.         'ignore',
  257.         'module',
  258.         'once',
  259.         'error']:
  260.         if a.startswith(action):
  261.             return a
  262.             continue
  263.     
  264.     raise _OptionError('invalid action: %r' % (action,))
  265.  
  266.  
  267. def _getcategory(category):
  268.     import re
  269.     if not category:
  270.         return Warning
  271.     
  272.     if re.match('^[a-zA-Z0-9_]+$', category):
  273.         
  274.         try:
  275.             cat = eval(category)
  276.         except NameError:
  277.             raise _OptionError('unknown warning category: %r' % (category,))
  278.         except:
  279.             None<EXCEPTION MATCH>NameError
  280.         
  281.  
  282.     None<EXCEPTION MATCH>NameError
  283.     i = category.rfind('.')
  284.     module = category[:i]
  285.     klass = category[i + 1:]
  286.     
  287.     try:
  288.         m = __import__(module, None, None, [
  289.             klass])
  290.     except ImportError:
  291.         raise _OptionError('invalid module name: %r' % (module,))
  292.  
  293.     
  294.     try:
  295.         cat = getattr(m, klass)
  296.     except AttributeError:
  297.         raise _OptionError('unknown warning category: %r' % (category,))
  298.  
  299.     if not isinstance(cat, types.ClassType) or not issubclass(cat, Warning):
  300.         raise _OptionError('invalid warning category: %r' % (category,))
  301.     
  302.     return cat
  303.  
  304. _processoptions(sys.warnoptions)
  305. simplefilter('ignore', category = OverflowWarning, append = 1)
  306. simplefilter('ignore', category = PendingDeprecationWarning, append = 1)
  307.